<template>
  <div class="parameter">
    <div class="header">
      <div class="left">
        <div class="title">参数设置</div>
      </div>
    </div>
    <div class="container">
      <el-table
        ref="tableData"
        :data="tableData"
        style="width: 100%"
        max-height="500"
      >
        <el-table-column prop="deviceName" label="设备名" align="center">
        </el-table-column>
        <el-table-column prop="systemName" label="设备码" align="center">
        </el-table-column>
        <el-table-column
          prop="paramString"
          label="参数"
          align="center"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <i
              class="el-icon-s-operation"
              style="color: #365eff; cursor: pointer; font-size: 22px"
              @click="edit(scope.row)"
            ></i>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="footer">
      <Pagination :total="total" @pagination="onPaginationChange"></Pagination>
    </div>
    <transition name="el-fade-in-linear">
      <div v-if="maskShow" class="transition-box mask">
        <div class="form">
          <div class="flex">
            <div class="title">参数设置</div>
            <div class="btn">
              <el-button size="small" @click="checkAll">全选</el-button>
              <el-button size="small" @click="checkOutAll">全不选</el-button>
            </div>
          </div>
          <el-form ref="ruleForm" label-position="top" label-width="80px">
            <el-form-item v-for="item in options" :key="item.id">
              <div>{{ item.argumentValue }}</div>
              <el-switch
                v-model="item.isCollect"
                :active-value="1"
                :inactive-value="0"
              />
            </el-form-item>
          </el-form>
          <div class="button">
            <el-button type="info" @click="cancel">取消</el-button>
            <el-button type="primary" @click="save">保存</el-button>
          </div>
        </div>
      </div>
    </transition>

    <success-tip v-if="success" :text="text"></success-tip>
  </div>
</template>

<script>
import { getAdviceAndParams, updateParam, getParams } from "@/api/device";
export default {
  name: "Parameter",
  data() {
    return {
      text: "保存成功",
      maskShow: false,
      success: false,
      tableData: [],
      total: 0,
      pageData: {
        pageSize: 10,
        pageNum: 1,
      },
      options: [],
      ids: [],
      params: [],
    };
  },
  methods: {
    checkAll() {
      this.options.forEach((item) => {
        item.isCollect = 1;
      });
    },
    checkOutAll() {
      this.options.forEach((item) => {
        item.isCollect = 0;
      });
    },
    getParamsData(data) {
      getParams({ type: data }).then((res) => {
        this.options = res.data;
      });
    },
    onPaginationChange({ page, limit }) {
      this.pageData.pageNum = page;
      this.getTableData();
    },
    getTableData() {
      getAdviceAndParams(this.pageData)
        .then((res) => {
          this.tableData = res.data.data;
          this.total = res.data.total;
        })
        .catch((err) => {
          this.tableData = [];
          this.total = 0;
        });
    },
    edit(item) {
      this.getParamsData(item.systemName);
      this.maskShow = true;
    },
    cancel() {
      this.maskShow = false;
      this.options = [];
    },
    save() {
      updateParam(this.options).then((res) => {
        if (res.code === 200) {
          this.successTip(this, "修改成功");
          this.maskShow = false;
        }
      });
    },
  },
  created() {
    this.getTableData();
  },
};
</script>
<style lang="scss">
.parameter {
  position: relative;
  padding: 36px 42px;

  .flex {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .header {
    display: flex;
    justify-content: space-between;

    .left {
      .title {
        height: 26px;
        line-height: 26px;
        font-size: 26px;
      }
    }
  }

  .container {
    margin-top: 36px;
    width: 100%;
    height: 688px;
    background: #ffffff;
    border-radius: 24px;
    overflow: hidden;
    box-shadow: 0px 0px 16px 0px rgba(0, 0, 0, 0.04);

    .el-table {
      height: 688px;
      max-height: 100% !important;
      font-size: 16px;

      &::before {
        height: 0;
      }

      .el-table__header-wrapper {
        .el-table__header th {
          padding: 0;
          height: 84px;
          line-height: 84px;
          background-color: #f7faff;
          font-size: 18px;
          border: none;
        }
      }

      .el-table__body-wrapper {
        height: 600px;
        max-height: 100% !important;

        tr {
          height: 80px;

          &:hover {
            td.el-table__cell {
              box-shadow: 0px 4px 16px 0px rgba(0, 0, 0, 0.06);
              background: #ffffff;
            }
          }
        }
      }
    }
  }

  .mask {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 10;
    width: 100%;
    height: 100%;
    background-color: rgba($color: #000000, $alpha: 0.5);

    .form {
      position: absolute;
      top: 10%;
      left: calc(50% - 455px);
      width: 910px;
      background: #ffffff;
      border-radius: 24px;
      overflow: hidden;

      .title {
        margin: 24px;
        height: 20px;
        line-height: 20px;
        font-size: 20px;
        font-weight: 500;
        color: #333333;
      }
      .btn {
        margin-right: 24px;
      }
      .el-form {
        margin: 0 auto;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        width: 862px;

        .el-form-item {
          margin-bottom: 20px;
          padding: 0 40px;
          width: 274px;
          height: 36px;
          background: #f7f7f7;
          border-radius: 12px;
          .el-form-item__content {
            display: flex;
            justify-content: flex-end;
            // justify-content: space-between;
            .el-switch {
              margin: 8px 0 8px 30px;
            }
          }
        }
      }

      .button {
        margin: 10px 0 36px;
        padding: 0 64px;
        display: flex;
        justify-content: space-around;
        width: 100%;
        height: 48px;

        .el-button {
          width: 204px;
          height: 48px;
          border-radius: 12px;
        }
      }
    }
  }
}
</style>
